Repository navigation
Examples: read the token from CONVERTAPI_TOKEN and find the sample files from any folder - #85
Merged
Merged
Conversation
…x300 The example already converts page 1 straight to JPG, but its comment still described the old extract-then-convert chain and linked pdf-to-extract, which is now the AI data-extraction converter. ScaleImage and ScaleProportions are not pdf -> jpg parameters and the API ignored them, so the 300x300 request came back 300x388 for test.pdf. FitToSize keeps the thumbnail inside 300x300 (231x300). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…les from any folder The examples reached Examples/TestFiles through paths relative to the working directory, three levels up in six places and four in nine. From bin/Debug/net10.0 only the four-level form reached TestFiles, from the project or repository folder neither did, and the backslash paths do not resolve on macOS or Linux. UploadOnceReuseTwice and Workflow climbed four levels from AppContext.BaseDirectory, and SplitPDF read files/sample.pdf from the working directory. Each example's .csproj now copies the sample files it uses to TestFiles/ next to the program, and Program.cs reads them through AppContext.BaseDirectory, so the examples run from dotnet run, Visual Studio and Rider, on any OS. The examples read the API token from the CONVERTAPI_TOKEN environment variable, the name the README quick start (#83) and the C# pages on convertapi.com use, instead of a hard-coded "api_token". Examples/README.md says how to set it and run an example. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Process.Start(path) uses UseShellExecute = false on .NET Core and later, so it tries to execute the saved PDF itself and throws Win32Exception on every OS (on macOS: "Permission denied"). The result was already saved and its path printed, so the example now ends there, like the other examples. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the example and documentation updates.
No open issues 🎉
What changed in this PR
Makes the SDK examples runnable without editing credentials or relying on the current working directory. Changes are limited to examples and their documentation; the SDK library is unchanged.
Changes:
- Read API tokens from
CONVERTAPI_TOKEN, consistent with the quick start in #83, and document setup. - Copy required sample files alongside each application and resolve them through
AppContext.BaseDirectory. - Include #82’s thumbnail sizing and comment corrections, and remove WatermarkPdf’s failing automatic file launch.
| File | Description |
|---|---|
| Examples/Workflow/Workflow.csproj | Copies the sample PDF to build output. |
| Examples/Workflow/Program.cs | Uses the environment token and output-relative sample path. |
| Examples/WatermarkPdf/WatermarkPdf.csproj | Copies both watermark input PDFs. |
| Examples/WatermarkPdf/Program.cs | Updates token and input paths; removes automatic file launch. |
| Examples/UploadOnceReuseTwice/UploadOnceReuseTwice.csproj | Copies the sample DOCX to build output. |
| Examples/UploadOnceReuseTwice/README.md | Documents environment-token setup and run commands. |
| Examples/UploadOnceReuseTwice/Program.cs | Replaces directory traversal with an output-relative sample path. |
| Examples/SplitPDF/Program.cs | Uses the environment token and locates its bundled PDF. |
| Examples/SplitAndMergePdf/SplitAndMergePdf.csproj | Copies the sample PDF to build output. |
| Examples/SplitAndMergePdf/Program.cs | Uses the environment token and output-relative sample path. |
| Examples/RetrieveUserInformation/Program.cs | Reads the token from the environment. |
| Examples/README.md | Adds shared setup, execution, and sample-file guidance. |
| Examples/MergeWordDocsToPdf/Program.cs | Updates token loading and both document paths. |
| Examples/MergeWordDocsToPdf/MergeWordDocsToPdf.csproj | Copies the DOCX and PowerPoint samples. |
| Examples/HandleRetry/Program.cs | Uses the environment token and output-relative sample path. |
| Examples/HandleRetry/HandleRetry.csproj | Copies the sample PDF to build output. |
| Examples/GetConverterInfoDemo/Program.cs | Reads the token from the environment. |
| Examples/FromStreamToStream/Program.cs | Reads the token from the environment. |
| Examples/ExceptionHandling/Program.cs | Updates token loading and the intentional invalid-input sample path. |
| Examples/ExceptionHandling/ExceptionHandling.csproj | Copies the DOCX used to demonstrate error handling. |
| Examples/DeleteFiles/Program.cs | Uses the environment token and output-relative sample path. |
| Examples/DeleteFiles/DeleteFiles.csproj | Copies the sample DOCX to build output. |
| Examples/CreatePdfThumbnail/Program.cs | Updates setup, corrects the comment, and uses FitToSize. |
| Examples/CreatePdfThumbnail/CreatePdfThumbnail.csproj | Copies the sample PDF to build output. |
| Examples/ConvertWordToPdfAndPng/Program.cs | Uses the environment token and output-relative sample path. |
| Examples/ConvertWordToPdfAndPng/ConvertWordToPdfAndPng.csproj | Copies the sample DOCX to build output. |
| Examples/ConvertUrlToPdf/Program.cs | Reads the token from the environment. |
| Examples/ConvertRemoteFile/Program.cs | Reads the token from the environment. |
| Examples/ConvertPdfToXlsx/Program.cs | Uses the environment token and output-relative sample path. |
| Examples/ConvertPdfToXlsx/ConvertPdfToXlsx.csproj | Copies the sample PDF to build output. |
| Examples/ConvertPdfToPptx/Program.cs | Uses the environment token and output-relative sample path. |
| Examples/ConvertPdfToPptx/ConvertPdfToPptx.csproj | Copies the sample PDF to build output. |
| Examples/ConvertPdfToPdfA/Program.cs | Updates token loading and invoice input paths. |
| Examples/ConvertPdfToPdfA/ConvertPdfToPdfA.csproj | Copies the invoice PDF and XML. |
| Examples/ConvertPdfToDocx/Program.cs | Uses the environment token and output-relative sample path. |
| Examples/ConvertPdfToDocx/ConvertPdfToDocx.csproj | Copies the sample PDF to build output. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #82. Please merge #82 first; until then this pull request also shows its one commit (3d70c44), which changes the thumbnail example's comment and parameters.
Problem
None of the examples ran as written:
Examples/TestFilesthrough paths relative to the working directory, written two ways: 6 uses climbed three levels (@"..\..\..\TestFiles\...") and 9 climbed four (@"..\..\..\..\TestFiles\..."). From the build output folderbin\Debug\net10.0, which is where Visual Studio starts a program, only the four-level form reachesExamples\TestFiles. From the repository or project folder, wheredotnet runstarts it, neither does. On macOS and Linux the backslash paths do not resolve at all. UploadOnceReuseTwice and Workflow climbed four levels fromAppContext.BaseDirectory, which breaks with any other output layout, for example a runtime-specificbin/Debug/net10.0/osx-arm64. SplitPDF readfiles/sample.pdfrelative to the working directory, sodotnet run --project Examples/SplitPDFfrom the repository root could not find it.new ConvertApi("api_token")(SplitPDF"api-token"), so each one had to be edited before it could run.Change
Each example's
.csprojcopies the sample files it uses toTestFiles/next to the program:and
Program.csreads them from there:That works from any working directory:
dotnet run --projectfrom the repository root,dotnet runin the project folder, Visual Studio and Rider (output folder), on Windows, macOS and Linux. SplitPDF does the same with its ownfiles/sample.pdf, which its.csprojalready copied.The token comes from the
CONVERTAPI_TOKENenvironment variable, and the comment that says where to get a token stays:CONVERTAPI_TOKEN, not theAPI_TOKENthat the Go, Java, Node, Python and Ruby examples read, because the README quick start in README: code that runs as copied, a quick start and C# tasks; NuGet packs the same README #83 and the C# pages on convertapi.com setCONVERTAPI_TOKEN: a reader who sets it once can run the README snippets and these examples. If the variable is not set, the SDK stops withArgumentNullExceptionforapiToken.New
Examples/README.md: how to set the variable and run an example. The run steps inExamples/UploadOnceReuseTwice/README.mdno longer say to edit the token intoProgram.cs.Only the examples change, not the library, so there is no CHANGELOG entry.
Verification
Run on macOS (arm64) with .NET SDK 10.0.401 against the production API on 2026-11-09, with
CONVERTAPI_TOKENset and nothing edited.$TMPDIRis the local temp folder.dotnet build ConvertApi.slnand the CI builddotnet build ConvertApi.sln -c Release -p:ContinuousIntegrationBuild=trueboth succeed with 0 errors. The 3 warnings are in lines this pull request does not change (CS8603 and CS8625 in ConverterCatalogDemo, CS0618 in GetConverterInfoDemo). Each example'sbin/<Configuration>/net10.0/TestFilesholds exactly the files it uses.dotnet run --project Examples/<Name> < /dev/nullfrom the repository root and exited with code 0 (ExceptionHandling prints its intended 400 error). CreatePdfThumbnail also ran from the project folder, frombin/Debug/net10.0and from a folder outside the repository. Itstest.jpgis 231x300 px, 8,858 bytes, as in PDF thumbnail example: fix the comment, keep the thumbnail inside 300x300 #82.Unhandled exception. System.ComponentModel.Win32Exception (13): An error occurred trying to start process '$TMPDIR/watermarked-....pdf' with working directory '<repository>'. Permission denied.Process.Start(path)usesUseShellExecute = falseon .NET Core and later, so it fails on Windows too. The second commit removes that line.CreatePdfThumbnail, a DOCX example and an example with two sample files:
Output of the other 18 examples
Not changed here
ConvertApi.ApiBaseUrito the staging API (https://stag-v2.convertapi.com) right after it prints the production address, so the public example reads the staging catalog.any -> pdf, which is not in the API spec (Converter path '/convert/any/to/pdf' not found in OpenAPI document.), and calls the obsoleteGetConverterInfo(CS0618).web -> pdf), WatermarkPdf (pdf -> watermark,pdf -> watermark-overlay) and Workflow (jpg -> zip) still convert on production, but none of those paths is in the current spec (https://v2.convertapi.com/info/openapi, 332 paths), which listshtml -> pdfwithUrl,pdf -> text-watermark,pdf -> pdf-watermarkandfile -> zip.output/in the current folder, which is the repository root when it runs asdotnet run --project Examples/SplitPDF, and.gitignoredoes not cover it. The other examples save to the temp folder.SecretandApiKeyto the console.🤖 Generated with Claude Code